/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web.actions.usuarios;


import com.ejemplo.bt.monedas.services.MonedaServices;
import com.ejemplo.bt.monedas.vo.Moneda;
import com.web.actions.AbstractAction;
import com.web.actions.usuarios.vo.ManteMonedaActionForm;
import com.web.actions.usuarios.vo.MonedaEditorActionForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Usuario
 */
public class ManteMonedasAction extends AbstractAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
      //cargo directamente la lista de monedas
      MonedaServices services = new MonedaServices();
      
      ManteMonedaActionForm datosValidados = new ManteMonedaActionForm();

      List<Moneda> monedas = services.obtenerMonedas();
      
      for(Moneda m : monedas) {
    	  MonedaEditorActionForm monedaForm = new MonedaEditorActionForm();
    	  monedaForm.setIdMoneda(m.getIdMoneda());
          monedaForm.setMoneda(m.getMoneda());
    	  datosValidados.getMonedas().add(monedaForm);
      }

      request.setAttribute("datosValidados", datosValidados);
      request.setAttribute("cantRegistrosRecuperados",new Integer(monedas.size()));

      return mapping.findForward("success");
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return mapping.findForward("failure");
    }
    }
}
